<?php

/**
 * This is the model class for table "role".
 *
 * The followings are the available columns in table 'role':
 * @property integer $idrole
 * @property string $name
 * @property string $can_create_edit_roles
 * @property string $can_create_edit_users
 * @property string $can_create_edit_shifts
 * @property string $can_create_edit_qshifts
 * @property string $can_create_callbacks
 * @property string $can_edit_callbacks
 * @property string $can_create_shift_change
 * @property string $can_edit_shift_change
 * @property string $can_create_qshift_change
 * @property string $can_edit_qshift_change
 * @property integer $team_id
 *
 * The followings are the available model relations:
 * @property Team $team
 * @property User[] $users
 */
class Role extends CActiveRecord
{

	public static $booleanValues= array('0'=>'False', '1'=>'True');

	 public static function getBooleanValue($bin=null){
	 
		if($bin!==null)
			return self::$booleanValues[$bin];
		return self::$booleanValues;
	 }

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'role';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('team_id', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>45),
			array('can_create_edit_roles, can_create_edit_users, can_create_edit_shifts, can_create_edit_qshifts, can_create_callbacks, can_edit_callbacks, can_create_shift_change, can_edit_shift_change, can_create_qshift_change, can_edit_qshift_change, team_id', 'required'),
			array('can_create_edit_roles, can_create_edit_users, can_create_edit_shifts, can_create_edit_qshifts, can_create_callbacks, can_edit_callbacks, can_create_shift_change, can_edit_shift_change, can_create_qshift_change, can_edit_qshift_change', 'length', 'max'=>1),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idrole, name, can_create_edit_roles, can_create_edit_users, can_create_edit_shifts, can_create_edit_qshifts, can_create_callbacks, can_edit_callbacks, can_create_shift_change, can_edit_shift_change, can_create_qshift_change, can_edit_qshift_change, team_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'team_relation' => array(self::BELONGS_TO, 'Team', 'team_id'),
			'relusers_relation' => array(self::HAS_MANY, 'User', 'idrole'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idrole' => 'Id',
			'name' => 'Name',
			'can_create_edit_roles' => 'Can Create Edit Roles',
			'can_create_edit_users' => 'Can Create Edit Users',
			'can_create_edit_shifts' => 'Can Create Edit Shifts',
			'can_create_edit_qshifts' => 'Can Create Edit Qshifts',
			'can_create_callbacks' => 'Can Create Callbacks',
			'can_edit_callbacks' => 'Can Edit Callbacks',
			'can_create_shift_change' => 'Can Create Shift Change',
			'can_edit_shift_change' => 'Can Edit Shift Change',
			'can_create_qshift_change' => 'Can Create Qshift Change',
			'can_edit_qshift_change' => 'Can Edit Qshift Change',
			'team_id' => 'Team',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search($team_id=null)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		
		if($team_id != null)
			$criteria->condition = 'team_id = '.$team_id;

		$criteria->compare('idrole',$this->idrole);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('can_create_edit_roles',$this->can_create_edit_roles,true);
		$criteria->compare('can_create_edit_users',$this->can_create_edit_users,true);
		$criteria->compare('can_create_edit_shifts',$this->can_create_edit_shifts,true);
		$criteria->compare('can_create_edit_qshifts',$this->can_create_edit_qshifts,true);
		$criteria->compare('can_create_callbacks',$this->can_create_callbacks,true);
		$criteria->compare('can_edit_callbacks',$this->can_edit_callbacks,true);
		$criteria->compare('can_create_shift_change',$this->can_create_shift_change,true);
		$criteria->compare('can_edit_shift_change',$this->can_edit_shift_change,true);
		$criteria->compare('can_create_qshift_change',$this->can_create_qshift_change,true);
		$criteria->compare('can_edit_qshift_change',$this->can_edit_qshift_change,true);
		$criteria->compare('team_id',$this->team_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Role the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
